package com.kaspersky.kaspressample.device_tests

import android.Manifest
import android.content.res.Configuration
import androidx.test.ext.junit.rules.activityScenarioRule
import androidx.test.rule.GrantPermissionRule
import com.kaspersky.kaspressample.MainActivity
import com.kaspersky.kaspressample.screen.MainScreen
import com.kaspersky.kaspressample.utils.SafeAssert.assertTrueSafely
import com.kaspersky.kaspressample.web.WebViewActivity
import com.kaspersky.kaspresso.device.exploit.Exploit
import com.kaspersky.kaspresso.testcases.api.testcase.TestCase
import org.junit.Assert.assertNull
import org.junit.Rule
import org.junit.Test

class DeviceExploitSampleTest : TestCase() {

    @get:Rule
    val runtimePermissionRule: GrantPermissionRule = GrantPermissionRule.grant(
        Manifest.permission.WRITE_EXTERNAL_STORAGE,
        Manifest.permission.READ_EXTERNAL_STORAGE
    )

    @get:Rule
    val activityRule = activityScenarioRule<MainActivity>()

    @Test
    fun exploitSampleTest() {
        before {
            device.exploit.setOrientation(Exploit.DeviceOrientation.Portrait)
            device.exploit.setAutoRotationEnabled(true)
        }.after {
            device.exploit.setOrientation(Exploit.DeviceOrientation.Portrait)
            device.exploit.setAutoRotationEnabled(true)
        }.run {

            step("Change orientation manually") {
                device.exploit.setOrientation(Exploit.DeviceOrientation.Landscape)
                activityRule.scenario.onActivity { activity ->
                    assertTrueSafely {
                        Configuration.ORIENTATION_LANDSCAPE ==
                                activity.resources.configuration.orientation
                    }
                }

                device.exploit.setOrientation(Exploit.DeviceOrientation.Portrait)
                activityRule.scenario.onActivity { activity ->
                    assertTrueSafely {
                        Configuration.ORIENTATION_PORTRAIT ==
                                activity.resources.configuration.orientation
                    }
                }
            }

            step("Rotate device") {
                device.exploit.rotate()
                activityRule.scenario.onActivity { activity ->
                    assertTrueSafely {
                        Configuration.ORIENTATION_LANDSCAPE ==
                                activity.resources.configuration.orientation
                    }
                }
            }

            step("Open web view and press back button") {
                MainScreen {
                    flakySafely {
                        webViewButton {
                            click()
                        }
                        device.activities.isCurrent(WebViewActivity::class.java)
                    }
                }

                device.exploit.pressBack(true)
                device.activities.isCurrent(MainActivity::class.java) // Asserts that MainActivity is resumed
            }

            step("Press home button") {
                device.exploit.pressHome()
                assertNull(device.activities.getResumed())
            }
        }
    }
}
